<style>
  .label {
    display: inline-block;
    margin: 10px 10px 0 0;

  }
</style>
<template>
  <div class="mod-config">
    <el-card class="box-card">
        <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
          <el-row>
            <el-col :span="6">
              <el-form-item label="标题">
                <el-input v-model="title" placeholder="标题" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="品牌">
               <el-select filterable v-model="brandId" placeholder="请选择" clearable>
                <el-option
                  v-for="item in brandList"
                  :key="item.id"
                  :value="item.name">
                </el-option>
              </el-select>
            </el-form-item>
            </el-col>    
            <el-col :span="6">
              <el-form-item label="买手">
                <el-select filterable v-model="nickname" placeholder="请选择" clearable>
                  <el-option
                    v-for="item in buyersList"
                    :key="item.uid"
                    :value="item.nickname">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
            </el-col>
          </el-row>
          
          <el-row>
            <el-col :span="6">
              <el-form-item label="分类">
               <el-select filterable v-model="categoryId" placeholder="请选择" @change="category1Change" clearable>
                <el-option
                  v-for="item in categoryParentList"
                  :key="item.id"
                  :value="item.id"
                  :label="item.name">
                </el-option>
              </el-select>
            </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="小类">
                <el-select filterable v-model="category2Id" placeholder="请选择" clearable>
                  <el-option
                    v-for="item in category2List"
                    :key="item.id"
                    :value="item.id"
                    :label="item.name">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="时间">
                <el-date-picker
                    v-model="time"
                    type="daterange"
                    range-separator="至"
                    value-format="yyyy-MM-dd"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item>
            <div style="padding-top: 0px">
              <el-button @click="getDataList('search')">查询</el-button>
              <el-button v-if="isAuth('biz:publishbuyers:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
            </div>
          </el-form-item>
        </el-form>
    </el-card>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label="序号">
      </el-table-column>
      <el-table-column
        width = "120"
        header-align="center"
        align="center"
        label="图片">
        <template slot-scope="scope">
          <img :src="scope.row.titlePic" alt="" width="100">
        </template>
      </el-table-column>
      <el-table-column
        prop="nickName"
        header-align="center"
        align="center"
        label="买手名称">
      </el-table-column>
      <el-table-column
        prop="title"
        header-align="center"
        align="center"
        label="标题" show-overflow-tooltip>
      </el-table-column>
      
      <el-table-column
        prop="brand"
        header-align="center"
        align="center"
        label="品牌">
      </el-table-column>
      <el-table-column
        prop="type"
        header-align="center"
        align="center"
        label="分类">
      </el-table-column>
      <el-table-column
        prop="sku"
        header-align="center"
        align="center"
        label="SKU">
      </el-table-column>
      <el-table-column
        prop="color"
        header-align="center"
        align="center"
        label="颜色">
      </el-table-column>
      <el-table-column
        prop="size"
        header-align="center"
        align="center"
        label="尺寸">
      </el-table-column>
      <el-table-column
        prop="sex"
        header-align="center"
        align="center"
        label="性别">
      </el-table-column>
      <el-table-column
        prop="price"
        header-align="center"
        align="center"
        label="价格">
      </el-table-column>
      <el-table-column
        prop="discount"
        header-align="center"
        align="center"
        label="折扣">
      </el-table-column>
      <el-table-column
        prop="publishTime"
        header-align="center"
        align="center"
        label="发布时间">
      </el-table-column>
      <el-table-column
        prop="receiveTime"
        header-align="center"
        align="center"
        label="预计到货时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        label="操作">
        <template slot-scope="scope">
          <!-- <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button> -->
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './publishbuyers-add-or-update'
  export default {
    data () {
      return {
        dataForm: {
          key: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        categoryParentList: [],
        title:"",
        category2Id:"",
        category2List: [],
        time:[],
        nickname:"",
        brandId:"",
        categoryId:"",
        buyersList:[],
        brandList:[],
        categoryList:[],
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList();
      this.$http({
          url: this.$http.adornUrl(`/biz/common/getBrands`),
          method: 'get',
          params: this.$http.adornParams()
      }).then(({data}) => {
          console.log(data);
          this.brandList = data.data;
      })
      this.$http({
          url: this.$http.adornUrl(`/biz/common/getCategories`),
          method: 'get',
          params: this.$http.adornParams()
      }).then(({data}) => {
          this.categoryList = data.data;
          for (var i = 0; i < this.categoryList.length; i++) {
            if (this.categoryList[i].fatherId == 0) {
                this.categoryParentList.push(this.categoryList[i]);
            }        
          }
      })
      this.$http({
          url: this.$http.adornUrl(`/biz/common/getBuyers`),
          method: 'get',
          params: this.$http.adornParams()
      }).then(({data}) => {
          console.log(data);
          this.buyersList = data.data;
      })
    },
    methods: {
      category1Change(value) {
        this.category2List = [];
        if (value == "") {
          this.category2Id = "";
          return;
        }
        this.category2Id = ""
        for (var i = 0; i < this.categoryList.length; i++) {
            if (this.categoryList[i].fatherId == value) {
                this.category2List.push(this.categoryList[i]);
            }        
          }
      },
      // 获取数据列表
      getDataList (val) {
        if(val == 'search'){
            this.pageIndex = 1;
        }
        var uid = "";
        if(this.nickname){
            this.buyersList.map(con => {
              if(this.nickname == con.nickname){
                uid = con.uid;
                return;
              } 
            });
        }

        var brandId = "";

        if(this.brandId){
            this.brandList.map(con => {
              if(this.brandId == con.name){
                brandId = con.id;
                return;
              } 
            });
        }
        

        /*var categoryId = "";
        if(this.categoryId){
            this.categoryList.map(con => {
              if(this.categoryId == con.name){
                categoryId = con.id;
                return;
              } 
            });
        }*/
        this.dataListLoading = true

        this.$http({
          url: this.$http.adornUrl('/biz/publishbuyers/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'brandId': brandId,
            'uid': uid,
            'categoryId': this.categoryId,
            'category2Id': this.category2Id,
            'title': this.title,
            'minDt':this.time!=undefined&&this.time.length == 2 ?this.time[0]:"",
            'maxDt':this.time!=undefined&&this.time.length == 2 ?this.time[1]:"",
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/biz/publishbuyers/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
